/*
 * $Id: SignInController.java 46 2013-05-03 21:38:12Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 46 $
 * $Date: 2013-05-03 21:38:12 +0000 (Fri, 03 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.web.controller;

import com.sci.logisticsmap.service.model.SignInChangePasswordDTCommand;
import com.sci.logisticsmap.service.model.SignUpDTCommand;
import com.sci.logisticsmap.web.model.SignInChangePasswordVWCommand;
import com.sci.logisticsmap.web.model.SignUpVWCommand;
import com.sci.logisticsmap.support.validator.CompoundValidator;
import com.sci.logisticsmap.web.support.validator.SignInChangePasswordVWCommandValidator;
import com.sci.logisticsmap.web.support.validator.SignInVWCommandValidator;
import com.sci.logisticsmap.web.support.validator.SignUpVWCommandValidator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.PostConstruct;

/**
 * The Controller for processing sign in view/ajax requests.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 46 $
 */
@Controller
@RequestMapping("")
public class SignInController extends BaseController {
    /**
     * The sign in path.
     */
    public static final String PATH_SIGN_IN = "/sign-in";
    /**
     * The sign in change password path.
     */
    public static final String PATH_SIGN_IN_CHANGE_PASSWORD = "/sign-in-change-password";
    /**
     * The sign out path.
     */
    public static final String PATH_SIGN_OUT = "/sign-out";

    /**
     * The sign in view.
     */
    private static final String viewSignIn = "signInPage";
    /**
     * The sign in change password view.
     */
    private static final String viewSignInChangePassword = "signInChangePasswordPage";
//    /**
//     * The sign out view.
//     */
    //private static final String viewSignOut = "signOut";

    /**
     * The default constructor.
     * Initializes a Controller class.
     */
    public SignInController() {
    }

    /*
     * (non-Javadoc)
     *
     * @see BaseController#init()
     */
    @PostConstruct
    public void init() {
        super.init();
    }

    /*
     * (non-Javadoc)
     *
     * @see org.springframework.web.bind.annotation.InitBinder
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new CompoundValidator(new SignInVWCommandValidator(), new SignInChangePasswordVWCommandValidator(), new SignUpVWCommandValidator()));
    }

    /**
     * The method to response on the sign in view request.
     *
     * @param  model
     *         The model.
     *
     * @return the sign in view.
     */
    @RequestMapping(value=PATH_SIGN_IN, method={RequestMethod.GET})
    public String onSignIn(Model model) {
        try {
            SignUpDTCommand signUpCommand = new SignUpDTCommand();
            model.addAttribute("signUpCommand", conversionUtilities.convert(signUpCommand, SignUpVWCommand.class));
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewSignIn;
    }

    /**
     * The method to response on the sign in view submitting request.
     *
     * @param  error
     *         The error flag.
     * @param  model
     *         The model.
     *
     * @return the sign in view.
     */
    @RequestMapping(value=PATH_SIGN_IN, method={RequestMethod.POST})
    public String onSignInSubmit(@RequestParam(value="error", required=false) boolean error, Model model) {
        try {
            if (error) {
                model.addAttribute("errorMessage", error);
            }
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewSignIn;
    }

    /**
     * The method to response on the sign in change password view request.
     *
     * @param  model
     *         The model.
     *
     * @return the sign in change password view.
     */
    @RequestMapping(value=PATH_SIGN_IN_CHANGE_PASSWORD, method={RequestMethod.GET})
    public String onSignInChangePassword(Model model) {
        try {
            SignInChangePasswordDTCommand signInChangePasswordCommand = new SignInChangePasswordDTCommand();
            model.addAttribute("signInChangePasswordCommand", conversionUtilities.convert(signInChangePasswordCommand, SignInChangePasswordVWCommand.class));
        } catch (Exception e) {
            addExceptionToModel(model, e);
        }

        return viewSignInChangePassword;
    }
}
